from app import db
from app.models import Teacher, Student, Course, Score
from werkzeug.security import generate_password_hash
import random
import datetime

def init_db():
    """初始化数据库，填充测试数据"""
    # 清空已有数据
    db.session.query(Score).delete()
    db.session.query(Student).delete()
    db.session.query(Course).delete()
    db.session.query(Teacher).delete()
    db.session.commit()
    
    # 创建教师
    teachers = [
        {
            'name': '张老师',
            'email': 'zhang@example.com',
            'password': generate_password_hash('password123'),
            'subject': '数学'
        },
        {
            'name': '李老师',
            'email': 'li@example.com',
            'password': generate_password_hash('password123'),
            'subject': '语文'
        },
        {
            'name': '王老师',
            'email': 'wang@example.com',
            'password': generate_password_hash('password123'),
            'subject': '英语'
        }
    ]
    
    for teacher_data in teachers:
        teacher = Teacher(**teacher_data)
        db.session.add(teacher)
    
    db.session.commit()
    
    # 获取创建的教师
    all_teachers = Teacher.query.all()
    
    # 创建课程
    courses = [
        {'name': '数学', 'description': '高中数学课程'},
        {'name': '语文', 'description': '高中语文课程'},
        {'name': '英语', 'description': '高中英语课程'},
        {'name': '物理', 'description': '高中物理课程'},
        {'name': '化学', 'description': '高中化学课程'},
        {'name': '生物', 'description': '高中生物课程'}
    ]
    
    for course_data in courses:
        course = Course(**course_data)
        db.session.add(course)
    
    db.session.commit()
    
    # 获取创建的课程
    all_courses = Course.query.all()
    
    # 创建学生
    grades = ['高一', '高二', '高三']
    students = []
    
    for i in range(1, 51):  # 创建50个学生
        student_data = {
            'name': f'学生{i}',
            'email': f'student{i}@example.com',
            'password': generate_password_hash('password123'),
            'grade': random.choice(grades),
            'teacher_id': random.choice(all_teachers).id
        }
        student = Student(**student_data)
        db.session.add(student)
        students.append(student)
    
    db.session.commit()
    
    # 创建成绩
    for student in Student.query.all():
        # 每个学生有多个科目的成绩
        for course in all_courses:
            # 每个科目有多次考试
            for test_number in range(1, 6):  # 1-5次考试
                # 随机生成成绩，60-100之间
                score_value = round(random.uniform(60, 100), 1)
                # 随机生成考试日期，过去一年内
                days_ago = random.randint(0, 365)
                test_date = datetime.datetime.now() - datetime.timedelta(days=days_ago)
                
                score = Score(
                    student_id=student.id,
                    course_id=course.id,
                    score_value=score_value,
                    test_number=test_number,
                    test_date=test_date
                )
                db.session.add(score)
    
    db.session.commit()
    print("数据库初始化完成！") 